//
//  HomeRefreshControl.swift
//  DSWeibo
//
//  Created by J on 16/5/1.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit

class HomeRefreshControl: UIRefreshControl {


    
    override init(){
        super.init()
        //1.初始化UI
        setupUI()
        
    }
    
    private func setupUI(){
        //1.添加子控件
        addSubview(refreshView)
        //2.布局子控件
        refreshView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            make.size.equalTo(CGSize(width: 160, height: 60))
        }
        
        
        
        //当用户下拉到一定程度的时候旋转箭头
        
        //下拉刷新触发刷新方法的时候 显示刷新界面
        
        
        //越往下 值越小  越往上 值越大
        
        addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions.New, context: nil)
        
        
    }
    

    
    //定义变量记录是否需要旋转提示箭头
    private var rotationArrowFlag = false
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        print(frame.origin.y)
        let Y = frame.origin.y
        
        if Y == 0 {
            return
        }
        
        
        //判断是否已经触发刷新事件
        if refreshing {
            refreshView.startLoadingViewAnim()
            //显示圈圈 并且执行动画
            print("显示圈圈 并且执行动画")
            return
        }
        
        
        if Y >= -50 && rotationArrowFlag{

            print("旋转")
            rotationArrowFlag = false
            refreshView.rottionArrowIcon(rotationArrowFlag)
            
        }else if Y < -50 && !rotationArrowFlag {
            //旋转
            print("旋转")
            rotationArrowFlag = true
            refreshView.rottionArrowIcon(rotationArrowFlag)
        }
        
    }
    
    
    
    override func endRefreshing() {
        super.endRefreshing()//调用super的用来结束动画
        refreshView.endRefreshAnim()//停止圆圈旋转
        rotationArrowFlag = false//复位是否需要旋转的标记
    }
    
    
    
    deinit{
        removeObserver(self, forKeyPath: "frame")
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    
    //MARK: -懒加载
    private lazy var refreshView: HomeRefreshView = {
        HomeRefreshView.refreshView()
        
    }()
    
    
    
}




 class HomeRefreshView: UIView{
    @IBOutlet weak var loadView: UIImageView!
    @IBOutlet weak var topView: HomeRefreshView!
    
    @IBOutlet weak var arrowIcon: UIImageView!
    class  func refreshView() -> HomeRefreshView{
     return NSBundle.mainBundle().loadNibNamed("RefreshView", owner: nil, options: nil).last as! HomeRefreshView//这个方法 当前类不能为私有
    }
    
    
    
    
    func startLoadingViewAnim(){//开始圈圈动画
        if  topView.hidden == true {
            return
        }
        
        
        topView.hidden = true
        
            //1.创建动画
            let anim = CABasicAnimation(keyPath: "transform.rotation")
            //2.设置动画属性
            anim.toValue = 2*M_PI
            anim.duration = 3
            anim.repeatCount = MAXFLOAT
            //该属性默认为YES,代表动画执行完毕就移除
            anim.removedOnCompletion = false
            //3.将动画添加到图层上
            loadView.layer.addAnimation(anim, forKey: nil)

    }
    
    func endRefreshAnim(){
        topView.hidden = false
         loadView.layer.removeAllAnimations()
    }
    
    
    //旋转箭头
    func rottionArrowIcon(flag:Bool){
    
        var angle = M_PI
        angle += flag ? -0.01 : 0.01
        UIView.animateWithDuration(0.25) { 
           self.arrowIcon.transform = CGAffineTransformRotate(self.arrowIcon.transform, CGFloat(angle))
        }
        
    }
    
    
}
